package com.xgpro.common.listener;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiCallBackDeleteCallBackRequest;
import com.dingtalk.api.request.OapiCallBackRegisterCallBackRequest;
import com.dingtalk.api.response.OapiCallBackRegisterCallBackResponse;
import com.xgpro.common.dingtalk.auth.AuthHelper;
import com.xgpro.domain.rolemanage.entity.Token;
import com.xgpro.pc.rolemanage.service.TokenService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;

/**
 * @program: xgpro__dev
 * @description: 存入钉钉token
 * @author: likai
 * @create: 2019-11-03 21:25
 **/

@Component
public class DDTokenListener implements ApplicationRunner {

    @Value("${DD_ENV.APP_KEY}")
    private String APP_KEY;

    @Value("${DD_ENV.APP_SECRET}")
    private String APP_SECRET;
    @Value("${DD_ENV.CALL_URL}")
    private String CALL_URL;

    @Value("${DD_ENV.ENCODING_AES_KEY}")
    private String ENCODING_AES_KEY;

    @Value("${DD_ENV.TOKEN}")
    private String TOKEN;

    @Resource
    private TokenService tokenService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Token token = tokenService.findToken();

        if (ObjectUtils.isEmpty(token)){
            //获取钉钉token存入到数据库
            //1.获取token
            try {
                String accessToken = AuthHelper.getAccessToken(APP_KEY, APP_SECRET);
                if (!StringUtils.isEmpty(accessToken)){
                    String ticket = AuthHelper.getJsapiTicket(accessToken);
                    System.out.println("ticket" + ticket);
                    Token tokenBean = new Token();
                    tokenBean.setToken(accessToken);
                    tokenBean.setDate(new Date());
                    if (!StringUtils.isEmpty(ticket)){
                        tokenBean.setTicket(ticket);
                    }
                    tokenService.insert(tokenBean);
                }


            } catch (Exception e) {
                System.out.println("获取钉钉token失败："+e.getMessage());
            }

        }
    }

    private void registerCallback() throws Exception {

        String accessToken = AuthHelper.getAccessToken(APP_KEY, APP_SECRET);
        /**
         * 先删除企业已有的回调
         */
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/delete_call_back");
        OapiCallBackDeleteCallBackRequest request = new OapiCallBackDeleteCallBackRequest();
        request.setHttpMethod("GET");
        client.execute(request, accessToken);
        client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/register_call_back");
        OapiCallBackRegisterCallBackRequest register = new OapiCallBackRegisterCallBackRequest();
        register.setUrl(CALL_URL);
        register.setAesKey(ENCODING_AES_KEY);
        register.setToken(TOKEN);
        register.setCallBackTag(Arrays.asList("user_add_org", "user_modify_org", "user_leave_org"));
        OapiCallBackRegisterCallBackResponse response = client.execute(register,accessToken);

        System.out.println("注册回调的结果是---------------"+response.getErrmsg());

    }
}
